<template>
  <el-container class="home-container">
    <el-header>
      <div class="header-left">
        <img src="../assets/logo.png" alt />
        <span>电商管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'" >
        <div class="collpaseBtn" @click='toggleCollapse'>|||</div>
        <el-menu unique-opened
        :collapse='isCollapse'
        :collapse-transition='false'
          class="el-menu-vertical-demo"
          background-color="#373C41"
          text-color="#fff"
          active-text-color="#409EFF"
          router
          :default-active='activePath'
        >
        <!-- 一级菜单 -->
          <el-submenu  :index="item.id+''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconList[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item  :index="'/'+subItem.path" @click = saveNavState(subItem.path)  v-for="subItem in item.children" :key="subItem.id">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      activePath: '',
      isCollapse: false,
      menuList: [],
      iconList: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpingouwudai2',
        102: 'iconfont icon-danju-tianchong',
        145: 'iconfont icon-quxianchengxian'
      }
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    saveNavState(activePath) {
      console.log(activePath)
      window.sessionStorage.setItem('activePath', '/' + activePath)
      this.activePath = '/' + activePath
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      // console.log(this.menuList)
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373c41;
  // color:#fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  .header-left {
    display: flex;
    align-items: center;
    span {
      color: #fff;
      font-size: 24px;
      margin-left: 20px;
    }
    img {
      width: 60px;
      height: 60px;
    }
  }
}
.el-aside {
  // color:#fff;
  background-color: #373c41;
  .el-menu {
    border:0;
  }
  .collpaseBtn {
    text-align: center;
    color:#909399;
    letter-spacing: 3px;
    cursor:pointer;
    -webkit-user-select: none;
  }
  .main{
    background-color: #EAEDF2;
  }
}
.iconfont {
  margin-right:10px;
}
</style>
